import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';
import '../utils/utils.dart';

class TaskPage extends StatefulWidget  {
  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<TaskPage> {
  Map taskObj = {};
  bool loadSuccess = false;

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  // 加载数据
  _loadData() async {
    DioUtils.request("Todolist/getTodoListData",
      context: context,
      method: "POST",
      onSuccess: (res) {
        if(res['code'] == 1) {
          setState(() {
            taskObj = res["data"];
            loadSuccess = true;
          });
        }else {
          Utils.showText(text: res['msg']);
        }
      },
      onError: (error) {}
    );
  }

  Widget _buildCard(Map item, String title, String route, String imgUrl, [String model]) {
    if(loadSuccess) {
      return GestureDetector(
        onTap: () {
          if(model.isNotEmpty) {
            Navigator.pushNamed(context, route, arguments: {'model': model});
          }else {
            Navigator.pushNamed(context, route);
          }
        },
        onPanDown: (details) {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          width: 700.w,
          margin: EdgeInsets.only(top: 16.w),
          padding: EdgeInsets.fromLTRB(24.w, 17, 16.w, 17),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.w),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(bottom: 10.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    // Image.asset('assets'+ imgUrl, width: 28),
                    Container(
                      margin: EdgeInsets.only(right: 5),
                      child: Text(item['count'].toString(), style: TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 23,
                        fontWeight: FontWeight.w500
                      )),
                    )
                  ],
                )
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(top: 13.w),
                    child: Text('${title}（代办工单数）', 
                      maxLines: 1, 
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: Color(0xFF666666),
                        fontSize: 12,
                        height: 1
                      )
                    ),
                  ),
                ],
              ),
            ]
          )
        )
      );
    }else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return Scaffold(
      appBar: TopBar(
        title: '代办任务'
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.fromLTRB(25.w, 23.w, 25.w, 20),
          child: Column(
            children: <Widget>[
              _buildCard(taskObj['enter_orders'], '物资入库', '/enter', 'task1.png', ''),
              _buildCard(taskObj['storage_orders'], '物资暂存', '/enter', 'task2.png', ''),
              _buildCard(taskObj['goods_destroy_out'], '物资销毁出库', '/out_page', 'task3.png', 'destroy'),
              _buildCard(taskObj['goods_donation_out'], '物资捐赠出库', '/out_page', 'task4.png', 'donation'),
              _buildCard(taskObj['goods_return_out'], '物资退还出库', '/out_page', 'task2.png', 'return'),
              _buildCard(taskObj['goods_sample_out'], '物资取样出库', '/out_page', 'task3.png', 'sample'),
              _buildCard(taskObj['goods_auction_out'], '物资拍卖出库', '/out_page', 'task5.png', 'auction'),
            ],
          )
        )
      )
    );
  }
}